import Vue from 'vue'
import Vuex from 'vuex'
import utils from '@/utils/index.js'
import Cookie from 'vue-cookies'
import i18n from '@/utils/i18n.js'
Vue.use(Vuex)
// console.log('window::::::::', window);
// console.log('i18n', i18n, i18n.t('home.betSuccess'));

import {
	userInfo,
	userBalance,
	marketCurrencyMatches,
	loginTest
} from '@/api/home'
import {
	indexCurrency,
	commonCurrencylist
} from '@/api/my'


export default new Vuex.Store({
	state: {
		windowWidth: window.innerWidth,
		timeDown: 0,
		userInfo: localStorage.getItem('LOGIN_USER_INFO') && localStorage.getItem('LOGIN_USER_INFO') !=
			'[object Object]' ? JSON.parse(localStorage.getItem('LOGIN_USER_INFO')) : {},
		lang: localStorage.getItem('lang') || (navigator.browserLanguage || navigator.language || navigator
			.userLanguage).substring(0, 2),
		balance: '', // 余额
		tick: localStorage.getItem('Tick') || 'Tick',
		webSocketStatus: false, // 长连接是否开启
		hasLogin: false, // pc 登录
		hasRegister: false, // pc 注册
		hasDeposit: false, // pc 充值
		hasSetPassword: false, // pc 设置密码
		hasResetPassword: false, // pc 重置密码
		showTips: [],
		tickPrice: {}, // tick 价格
		oneMinPrice: {}, // 1分钟的最新数据
		matches: {},
		allMatches: [], // 全部的交易币种
		newMyDownOrder: {}, // 用户下单最新的一期
		closeMyDownOrder: {}, // 倒计时结束的订单
		newOrderPrice: {}, // 最新一期数据
		newOdd: [], // 最新赔率
		tipsMsg: {
			1: i18n.t('home.betSuccess'),
			2: i18n.t('home.frequent'),
			3: i18n.t('home.trading'),
			4: i18n.t('home.binary'),
		},
	},
	mutations: {
		decrementCountdown(state) {
			state.timeDown++
		},
		onChangeUserInfo(state, data) {
			// console.log('dddd', data);
			state.userInfo = data
			localStorage.setItem('LOGIN_USER_INFO', JSON.stringify(data))
			localStorage.setItem('TOKEN', data.token)
			state.userInfo = data
		},
		upDataLang(state, data) {
			state.lang = data
		},
		onChangeBalance(state, data) {
			state.balance = data
		},
		changeMatches(state, data) {
			state.matches = data
		},
		changeAllMatches(state, data) {
			state.allMatches = data
		},
		upDataMyDownOrder(state, data) {
			state.newMyDownOrder = data
		},
		upDataNewOrderPrice(state, data) {
			state.newOrderPrice = data
		},
		upDataOdd(state, data) {
			state.newOdd = data
		},
		closeMyOrderDom(state, data) {
			state.closeMyDownOrder = data
		},
		upDataTick(state, data) {
			state.tick = data
		},
		onChangeWebSocketStatus(state, data) {
			state.webSocketStatus = data
		},
		upDataTickPrice(state, data) {
			state.tickPrice = data
		},
		upDataOneMinPrice(state, data) {
			// console.log('data', data);
			state.oneMinPrice = data
		},
		/**
		 * @data 
		 * type: 1, // 1成功  2频繁访问 3交易更新 4接口其他报错
		 */
		onChangeTips(state, data) {
			let obj = {
				uid: utils.guid()
			}
			// if (data && typeof data === 'object') {
			// 	obj.type = data.type;
			// 	obj.msg = data.msg;
			// } else {
			// 	obj.type = data;
			// }
			// state.showTips.push({
			// 	...obj,
			// })
			obj.type = data.type || data;
			obj.msg = data.msg ? data.msg : state.tipsMsg[obj.type] || state.tipsMsg[4];
			console.log('obj', obj, state.tipsMsg, data);
			// setTimeout(() => {
			// 	let findIndex = state.showTips.findIndex(i => i.uid == obj.uid)
			// 	if (findIndex != -1) {
			// 		state.showTips.splice(findIndex, 1)
			// 	}
			// }, 3500)
			window.parent.postMessage({
				updateTips: obj,
			}, '*')
			console.log('state', state, data);
		},
		closeTips(state, data) {
			let findIndex = state.showTips.findIndex(i => i.uid == data)
			if (findIndex != -1) {
				state.showTips.splice(findIndex, 1)
			}
		},
		changeLogin(state, data) {
			state.hasLogin = data
		},
		changeRegister(state, data) {
			state.hasRegister = data
		},
		changeDeposit(state, data) {
			state.hasDeposit = data
		},
		changeSetPassword(state, data) {
			state.hasSetPassword = data
		},
		changeResetPassword(state, data) {
			state.hasResetPassword = data
		},
	},
	actions: {
		startCountdown({
			commit
		}) {
			setInterval(() => {
				commit('decrementCountdown')
			}, 1000)
		},
		async getUserInfo({
			commit,
			dispatch
		}) {
			const res = await userInfo()
			// console.log('个人信息：：', res);
			if (res && res.code == 1) {
				commit('onChangeUserInfo', res.data)
			} else if (res.code == 999) {
				dispatch('testLogin')
			}
		},
		async testLogin({
			commit
		}) {
			// 游客登录
			const testRes = await loginTest()
			// console.log('游客登录：：', testRes);
			if (testRes.code == 1) {
				commit('onChangeUserInfo', testRes.data)
				localStorage.setItem('TOKEN', testRes.data.token)
			}
		},
		async getMatches({
			commit
		}) {
			const res = await marketCurrencyMatches()
			if (res && res.code == 1) {
				// console.log('resgetMatches', res);
				let matches = localStorage.getItem('matches')
				if (matches) {
					commit('changeMatches', JSON.parse(matches))
				} else {
					commit('changeMatches', res.data[0].matches[0])
				}
				commit('changeAllMatches', res.data[0].matches)
			}
		},
		async getBalance({
			commit,
			state
		}) {
			window.parent.postMessage({
				updateBalance: true,
			}, '*')
			const res = await userBalance()
			if (res && res.code == 1) {
				commit('onChangeBalance', res.data.balance)
			}
		},
	},
	modules: {}
})